package com.wondream.myframework.app.utils;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author LW
 * @date 2021/3/9
 * @toDo
 */
public class BillBusiUtils {
    private static final String letterChar = "abcdef1234567890ABCDEF";
    private static final String numberChar = "0123456789";
    private static final String fullLetterChar = "abcdefghijklmnopqrstuvwxyz";
    private static final String allChar = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final byte FIX_KEY[] = "FC5B8717495251443A9A6D90AF59A3A4FC5B871749525144".getBytes();
    public static final byte[] keyiv = {8, 7, 6, 5, 4, 3, 2, 1};


    /**
     * 解密用户号
     *
     * @param userId
     * @param logger
     * @return
     */
    public static String decodeUserIdByDes3(String userId, Logger logger) {
        byte[] bytes;
        try {
            bytes = HyTreeDesPKCPaddingUtil.des3DecodeCBC(BillBusiUtils.FIX_KEY, BillBusiUtils.keyiv, HyTreeDesECBUtils.hexStringToBytes(userId));
            return new String(bytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    /**
     * 加密用户号
     *
     * @param userId
     * @param logger
     * @return
     */
    public static String encodeUserIdByDes3(String userId, Logger logger) {
        byte[] bytes;
        try {
            return HyTreeDesECBUtils.byte2HexStr(HyTreeDesPKCPaddingUtil.des3EncodeCBC(BillBusiUtils.FIX_KEY, BillBusiUtils.keyiv, userId.getBytes(StandardCharsets.UTF_8)));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }


    public static String replaceLetter(String o) {
        Pattern p = Pattern.compile("[a-zA-z]");
        Matcher matcher = p.matcher(o);
        // 把字母替换成 0
        String newStr = matcher.replaceAll("");
        return newStr;
    }


    public static void main(String[] args) {
        System.out.println("张婷：" + decodeUserIdByDes3("A4554A22B3100459BCEBEAFAE71BA642", null));
        System.out.println("张婷-生产：" + decodeUserIdByDes3("A4554A22B3100459E9C9C0136A91684B", null));
        System.out.println("李伟：" + decodeUserIdByDes3("A4554A22B3100459C1908E2F3D065CBC", null));
        System.out.println("李伟-生产：" + decodeUserIdByDes3("A4554A22B3100459F9199A962F8E34D2", null));
    }
}
